package com.marry.business.controller.active;

import cn.hutool.core.bean.BeanUtil;
import com.marry.business.domain.MlUser;
import com.marry.business.entity.Active;
import com.marry.business.service.active.ActiveService;
import com.marry.business.service.active.MlUserService;
import com.marry.business.vo.active.AddVo;
import com.marry.core.exception.BusinessException;
import com.marry.core.result.WrapMapper;
import com.marry.core.result.Wrapper;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @path：com.marry.business.controller.active.ActiveController.java
 * @className：ActiveController.java
 * @description：TODO
 * @author：LiuYun
 * @dateTime：2021/7/9 1:02
 * @editNote：
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("active")
public class ActiveController {

    @Autowired
    private MlUserService mlUserService;

    @Autowired
    private ActiveService activeService;


    @GetMapping("detail")
    public Mono<MlUser> detail(Long id){
        return mlUserService.detail(id);
    }

    @GetMapping("queryAll")
    public Flux<MlUser> queryAll(MlUser user){
        return mlUserService.findAll(user);
    }

    @PostMapping("save")
    public Mono<MlUser> save(MlUser user){
        return mlUserService.save(user);
    }

    @ApiOperation(value = "添加活动", notes = "")
    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    public  Flux<Active> add(@RequestBody  AddVo... addVos){
        Active[] actives = Arrays.stream(addVos).map(vo -> BeanUtil.toBean(vo, Active.class)).collect(Collectors.toList()).toArray(new Active[]{});
        Flux<Active> save = activeService.save(actives);
        Wrapper<String> result = WrapMapper.wrap(200, "获取成功", "添加成功");
        return save;
    }

}
